@page
@using Microsoft.AspNetCore.Mvc.Localization
@using Volo.Abp.AspNetCore.Mvc.UI.Layout
@using Volo.Abp.AspNetCore.Mvc.UI.Packages.Anchor
@using Volo.Abp.AspNetCore.Mvc.UI.Packages.Clipboard
@using Volo.Abp.AspNetCore.Mvc.UI.Packages.MalihuCustomScrollbar
@using Volo.Abp.AspNetCore.Mvc.UI.Packages.Popper
@using Volo.Abp.AspNetCore.Mvc.UI.Packages.Prismjs
@using Volo.Abp.AspNetCore.Mvc.UI.Theming
@using Volo.Docs
@using Volo.Docs.Localization
@using Volo.Docs.Pages.Documents.Project
@using Volo.Docs.Pages.Documents.Shared.ErrorComponent
@inject IThemeManager ThemeManager
@inject IPageLayout PageLayout
@inject IHtmlLocalizer<DocsResource> L
@model IndexModel
@{
    ViewBag.FluidLayout = true;
    Layout = ThemeManager.CurrentTheme.GetEmptyLayout();
    PageLayout.Content.Title = Model.DocumentName?.Replace("-", " ");
    ViewBag.Description = Model.GetDescription();
}
@section styles {
    <abp-style-bundle name="@typeof(IndexModel).FullName">
        <abp-script type="@typeof(PrismjsStyleBundleContributor)" />
        <abp-script type="@typeof(MalihuCustomScrollbarPluginStyleBundleContributor)" />
        <abp-style src="/Pages/Documents/Project/bootstrap-toc.css" />
        <abp-style src="/Pages/Documents/Shared/Styles/vs.css" />
    </abp-style-bundle>
}
@section scripts {
    <abp-script-bundle name="@typeof(IndexModel).FullName">
        <abp-script type="@typeof(MalihuCustomScrollbarPluginScriptBundleContributor)" />
        <abp-script type="@typeof(ClipboardScriptBundleContributor)" />
        <abp-script type="@typeof(AnchorJsScriptBundleContributor)" />
        <abp-script type="@typeof(PrismjsScriptBundleContributor)" />
        <abp-script type="@typeof(PopperJsScriptBundleContributor)" />
        <abp-script src="/Pages/Documents/Project/bootstrap-toc.js" />
        <abp-script src="/Pages/Documents/Shared/Scripts/vs.js" />
        <abp-script src="/Pages/Documents/Project/index.js" />
        <abp-script src="/Pages/Documents/Shared/ErrorComponent/error.js" />
    </abp-script-bundle>
}
@if (Model.LoadSuccess)
{
    <div class="docs-page" data-spy="scroll" data-target="#docs-sticky-index">
        <div class="row justify-content-end">
            <div class="col-md-3 docs-sidebar dark-sidebar">
                <div class="docs-sidebar-wrapper">

                    <div class="docs-top">
                        <nav class="navbar navbar-logo">
                            @if (!Model.Project.Name.IsNullOrWhiteSpace())
                            {
                                <a class="navbar-brand w-100" href="@Model.CreateVersionLink(Model.LatestVersionInfo, Model.GetSpecificVersionOrLatest())">
                                    <span id="ProjectName">@Model.Project.Name</span><br>
                                    <strong class="display-block">
                                        @L["Documents"]
                                    </strong>
                                </a>
                            }

                            @if (!Model.Document.Project.MainWebsiteUrl.IsNullOrWhiteSpace())
                            {
                                <a href="@Model.Document.Project.MainWebsiteUrl" class="go-back-site" id="GoToMainWebSite">
                                    <i class="fa fa-chevron-left"></i>
                                    @L["BackToWebsite"]
                                </a>
                            }

                            <div class="for-mobile">
                                <div class="navbar-dark">
                                    <button type="button" class="open-dmenu navbar-toggler" aria-label="Close">
                                        <span class="navbar-toggler-icon"></span>
                                    </button>
                                </div>
                            </div>
                        </nav>
                    </div>

                    <div class="docs-tree-list">

                        @if (Model.ShowProjectsCombobox && Model.ProjectSelectItems.Count > 1)
                        {
                            <div class="docs-version">
                                <div class="version-select">
                                    <div class="input-group">
                                        <div class="input-group-prepend">
                                            <label class="input-group-text">
                                                <i class="fa fa-folder-open" aria-hidden="true" data-toggle="tooltip" title="@L["Project"]"></i>
                                            </label>
                                        </div>

                                        <select asp-items="Model.ProjectSelectItems"
                                                class="form-control"
                                                onchange="window.location.pathname = this.value">
                                        </select>
                                    </div>
                                </div>
                            </div>
                        }

                        <div class="row">
                            @if (Model.VersionSelectItems.Any())
                            {
                                <div class="col @(Model.LanguageSelectListItems.Count > 1?"pr-0":"")">
                                    <div class="docs-version @(Model.LanguageSelectListItems.Count > 1?"pr-1":"")">
                                        <div class="version-select">
                                            <div class="input-group">
                                                <div class="input-group-prepend">
                                                    <label class="input-group-text">
                                                        <i class="fas fa-code-branch" aria-hidden="true" data-toggle="tooltip" title="@L["Version"]"></i>
                                                    </label>
                                                </div>

                                                <select asp-items="Model.VersionSelectItems"
                                                        class="form-control"
                                                        onchange="if (this.value) { window.location.replace(this.value) }">
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            }

                            @if (Model.LanguageSelectListItems.Count > 1)
                            {
                                <div class="col-5  @(Model.VersionSelectItems.Any()?"pl-0":"")">
                                    <div class="docs-version docs-language @(Model.VersionSelectItems.Any()?"pl-1":"")">
                                        <div class="version-select">
                                            <div class="input-group">
                                                @*<div class="input-group-prepend">
                                                        <label class="input-group-text">
                                                            <i class="fa fa-globe" aria-hidden="true" data-toggle="tooltip" title="@L["Language"]"></i>
                                                        </label>
                                                    </div>*@
                                                <select asp-items="Model.LanguageSelectListItems"
                                                        class="form-control"
                                                        onchange="window.location.replace(this.value)">
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            }
                        </div>

                        <div class="docs-version">
                            <div class="version-select">
                                <div class="input-group">
                                    <div class="input-group-prepend">
                                        <label class="input-group-text"><i class="fa fa-filter"></i></label>
                                    </div>

                                    <input class="form-control"
                                           id="filter"
                                           type="search"
                                           data-search-url="@Model."
                                           placeholder="@L["FilterTopics"].Value"
                                           aria-label="Filter">
                                </div>
                            </div>
                        </div>


                        @if (Model.Navigation == null || !Model.Navigation.HasChildItems)
                        {
                            <div class="text-muted p-3">
                                <i class="fa fa-warning"></i> @L["NavigationDocumentNotFound"]
                            </div>
                        }
                        else
                        {
                            <ul root-node="@Model.Navigation"
                                version="@(Model.LatestVersionInfo == null || Model.LatestVersionInfo.IsSelected ? DocsAppConsts.Latest : Model.Version)"
                                project-name="@Model.ProjectName"
                                project-format="@Model.Project.Format"
                                selected-document-name="@Model.DocumentNameWithExtension"
                                language="@Model.LanguageCode"
                                id="sidebar-scroll"
                                class="nav nav-list"></ul>
                        }
                    </div>

                </div>
            </div>

            @if (Model.Document != null)
            {
                <div class="col-md-7 docs-content bg-white">
                    <div class="docs-link-btns">
                        <div class="row">
                            <div class="col">
                                @if (Model.FullSearchEnabled)
                                {
                                    <div class="search-area">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <label class="input-group-text"><i class="fa fa-search"></i></label>
                                            </div>

                                            <input class="form-control"
                                                   id="fullsearch"
                                                   type="search"
                                                   data-fullsearch-url="/search/@Model.LanguageCode/@Model.ProjectName/@Model.Version/"
                                                   placeholder="@L["FullSearch"].Value"
                                                   aria-label="Filter">
                                        </div>
                                    </div>
                                }
                            </div>
                            <div class="col-auto">
                                <div class="cont-container">
                                    @if (Model.Document.Contributors != null && Model.Document.Contributors.Count > 0)
                                    {
                                        <span class="for-desktop mr-3">
                                            @(L["Contributors"].Value)
                                        </span>
                                        @foreach (var contributor in Model.Document.Contributors)
                                        {
                                            <a href="@contributor.UserProfileUrl" target="_blank" class="cont-avatar">
                                                <img src="@contributor.AvatarUrl"
                                                     class="rounded-circle"
                                                     alt="Avatar"
                                                     height="21"
                                                     width="21"
                                                     title="@contributor.Username" />
                                            </a>
                                        }
                                    }
                                </div>
                            </div>
                            <div class="col-auto">
                                @if (!string.IsNullOrEmpty(Model.Document.EditLink))
                                {
                                    <a href="@Model.Document.EditLink" target="_blank">
                                        <i class="fa fa-edit"></i>
                                        @(L["Edit"])
                                        <span class="for-desktop text-muted">@L["LastEditTime"]: @Model.Document.LastUpdatedTime.ToShortDateString()</span>
                                    </a>
                                }

                            </div>
                        </div>


                    </div>

                    <div class="docs-content-field">
                        <div class="docs-text-field">
                            <div class="position-relative">
                                @if (Model.DocumentPreferences != null && Model.DocumentPreferences.Parameters != null && Model.DocumentPreferences.Parameters.Any())
                                {
                                    <div class="alert alert-primary alert-criteria">
                                        <abp-row>
                                            <abp-column>
                                                <p class="alert-p mb-2">
                                                    <i class="fa fa-info-circle"></i>
                                                    @L["MultipleVersionDocumentInfo"]
                                                </p>
                                            </abp-column>
                                        </abp-row>
                                        <abp-form-row>
                                            @foreach (var parameter in Model.DocumentPreferences.Parameters)
                                            {
                                                <abp-column size="_4">
                                                    <div class="input-group">
                                                        <div class="input-group-prepend">
                                                            <span class="input-group-text" id="@("Section" + parameter.Name + "ComboboxAddonId")">@(parameter.DisplayName)</span>
                                                        </div>
                                                        <select class="doc-section-combobox form-control"
                                                                aria-describedby="@("Section" + parameter.Name + "ComboboxAddonId")"
                                                                id="@("Section" + parameter.Name + "ComboboxId")" data-key="@parameter.Name">
                                                            @foreach (var value in parameter.Values)
                                                            {
                                                                @if (value.Key == (Model.UserPreferences.ContainsKey(parameter.Name) ? Model.UserPreferences[parameter.Name] : null))
                                                                {
                                                                    <option value="@value.Key" selected="selected">@value.Value</option>
                                                                }
                                                                else
                                                                {
                                                                    <option value="@value.Key">@value.Value</option>
                                                                }
                                                            }
                                                        </select>
                                                    </div>
                                                </abp-column>
                                            }
                                        </abp-form-row>
                                    </div>
                                }
                            </div>

                            <div data-spy="scroll" data-target="#docs-sticky-index" data-offset="0">
                                <article class="docs-body">

                                    @if (Model.DocumentLanguageCode != Model.LanguageCode)
                                    {
                                        <abp-alert alert-type="Warning" dismissible="true" class="mb-0">
                                            @L["DocumentNotFoundInSelectedLanguage"]
                                        </abp-alert>
                                    }
                                    @Html.Raw(Model.Document.Content)
                                </article>

                                <div id="crawler_link" style="display:none;">

                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- Right Bar -->
                <div class="col-md-2 docs-page-index position-relative bg-light">
                    <div class="doc-social-btns">
                        @(L["ShareOn"].Value + " :")
                        <a href="#" target="_blank" class="share-button twitter" id="TwitterShareLink" title="Twitter">
                            <i class="fa fa-twitter"></i>
                        </a>
                        <a href="#" target="_blank" class="share-button linkedin" id="LinkedinShareLink" title="LinkedIn">
                            <i class="fa fa-linkedin"></i>
                        </a>
                        <a href="#" target="_blank" class="share-button email" id="EmailShareLink" title="E-mail">
                            <i class="fa fa-envelope-square"></i>
                        </a>
                    </div>
                    <div id="scroll-index" class="docs-inner-anchors mt-2">
                        <h5>@L["InThisDocument"]</h5>
                        <nav id="docs-sticky-index" class="navbar index-scroll">
                        </nav>

                        <div class="row">
                            <div class="col p-0 py-3">
                                <a href="javascript:;" class="scroll-top-btn"><i class="fa fa-chevron-up"></i> @L["GoToTop"]</a>
                            </div>
                        </div>
                    </div>
                </div>
            }
            else
            {
                <div class="col-md-9 p-5 bg-white">
                    <p>@L["DocumentNotFound"]</p>

                    <a href="/">
                        <i class="fa fa-chevron-left"></i>
                        @L["BackToWebsite"]
                    </a>
                </div>
            }
        </div>
    </div>
}
else
{
    if (!Model.ProjectFound)
    {
        @(await Component.InvokeAsync<ErrorViewComponent>(new
         {
             model = new ErrorPageModel
             {
                 RedirectUrl = Model.DocumentsUrlPrefix,
                 ErrorCode = "404",
                 ErrorMessage = L.GetString("ProjectNotFound")
             }
         }))
    }

    if (!Model.DocumentFound)
    {
        @(await Component.InvokeAsync<ErrorViewComponent>(new
          {
              model = new ErrorPageModel
              {
                  RedirectUrl = Model.DocumentsUrlPrefix + Model.LanguageCode + "/" + Model.ProjectName + "/"
                                + (Model.LatestVersionInfo.IsSelected ? DocsAppConsts.Latest : Model.Version),
                  ErrorCode = "404",
                  ErrorMessage = L.GetString("DocumentNotFound"),
                  AutoRedirect = !Model.DocumentName.IsNullOrWhiteSpace()
              }
          }))
    }
}
